var app = getApp()

Page({
  data: {
    //判断小程序的API，回调，参数，组件等是否在当前版本可用。
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    isShow: false
  },
  onLoad: function(op) {
    console.log('opop:', op)
    this.shareid = op.id
    wx.showLoading()
    let that = this
    // 查看是否授权
    wx.getSetting({
      success: function(res){
        wx.hideLoading()
        console.log(1)
        if (res.authSetting['scope.userInfo']) {
          console.log(res)
          //用户已经授权过
          that.getUserInfo()
        } else {
          that.setData({
            isShow: true
          })
        }
      }
    })
  },
  bindGetUserInfo: function(e) {
    let that = this
    console.log('userinfo: ', e.detail.userInfo)
    if (e.detail.userInfo){
      //用户按了允许授权按钮
      that.getUserInfo()
    } else {
      //用户按了拒绝按钮
      that.getFail()
    }
  },
  getUserInfo() {
    wx.showLoading()
    let that = this
    var a = {
        sessionid: "",
        wxInfo: "",
        memberInfo: ""
    };
    wx.login({
      success: (response) => {
        console.log(response)
        app.util.request({
          url: "auth/session/openid",
          data: {
              code: response.code
          },
          cachetime: 0,
          success: (r) => {
            if (!r.data.errno) {
              console.log(r)
              a.sessionid = r.data.data.sessionid;
              wx.setStorageSync("userInfo", a);
              wx.getUserInfo({
                success: function t(r) {
                  a.wxInfo = r.userInfo;
                  wx.setStorageSync("userInfo", a);
                  app.util.request({
                    url: "auth/session/userinfo",
                    data: {
                        signature: r.signature,
                        rawData: r.rawData,
                        iv: r.iv,
                        encryptedData: r.encryptedData
                    },
                    method: "POST",
                    header: {
                        "content-type": "application/x-www-form-urlencoded"
                    },
                    cachetime: 0,
                    success: function t(r) {
                      // console.log('r',r)
                      if (!r.data.errno) {
                          a.memberInfo = r.data.data;
                          wx.setStorageSync("userInfo", a)
                      }
                      typeof e == "function" && e(a)
                      // 获取到用户信息就跳转到主页面
                      console.log('that.shareid: ', that.shareid)
                      app.util.request({
                        url: 'entry/wxapp/share',
                        data: {
                          share_id: that.shareid
                        },
                        success: (res) => {
                          console.log(res.data.data.i_id)
                          app.globalData.i_id = res.data.data.i_id
                        }
                      })
                      wx.hideLoading()
                      wx.switchTab({
                        url: '../shoujiao/shoujiao'
                      })
                    }
                  })
                },
                fail: function e() {},
                complete: function e() {}
              })
            }
          }
        })
      },
      fail: function e() {
        that.getFail()
      }
    })
  },
  getFail() {
    wx.showModal({
      title: "获取信息失败",
      content: "请允许授权以便为您提供给服务",
      showCancel: false,
      success: function e(t) {
        // if (t.confirm) {
        //   util.getUserInfo()
        // }
      }
    })
  },
  share() {

  }
})
